﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using EShop.BLL;
using EShop.Common;

namespace EShop.Client
{
    public partial class frmMerchandiseStockLowAlertCheck : frmBase
    {
        public frmMerchandiseStockLowAlertCheck()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 总记录数量
        /// </summary>
        int iTotalCount = 0;
        /// <summary>
        /// 每页最大数量
        /// </summary>
        int iPageSize = 0;
        /// <summary>
        /// 总页数
        /// </summary>
        int iPageCount = 0;
        /// <summary>
        /// 当前页码
        /// </summary>
        int iCurrentPage = 0;
        /// <summary>
        /// 所有数据行
        /// </summary>
        /// 
        DataTable dt = new DataTable();

        int iDefaultAlertNum = 10;
        BLL.MerchandiseAlertBAL oMerchandiseAlertBAL = new MerchandiseAlertBAL();
        private void frmMerchandiseStockLowAlertCheck_Load(object sender, EventArgs e)
        {
            iPageSize = oShopConfigInfo.PageSize;
            bindingNavigatorMoveFirstItem.Enabled = true;
            bindingNavigatorMovePreviousItem.Enabled = true;
            bindingNavigatorMoveNextItem.Enabled = true;
            bindingNavigatorMoveLastItem.Enabled = true;
            bindingNavigatorPositionItem.Enabled = true;
            bindingNavigatorCountItem.Enabled = true;
            //cbxAll.Checked = false;
            dt = oMerchandiseAlertBAL.MerchandiseStockLowAlert(null, 0);
            Init();
        }

        private void Init()
        {
            dgvAlert.Rows.Clear();
            iTotalCount = dt.Rows.Count;
            iPageCount = iTotalCount / iPageSize;
            if (iTotalCount % iPageSize != 0) iPageCount += 1;
            bindingNavigatorCountItem.Text = "/" + iPageCount.ToString();
            BindData();
        }

        private void BindData()
        {
            if (iTotalCount <= 0) return;
            this.bindingNavigatorPositionItem.Text = (iCurrentPage + 1).ToString();
            int iPosBegin = iCurrentPage * iPageSize;
            int iPosEnd = 0;
            if (iCurrentPage >= iPageCount - 1)
            {
                iPosEnd = iTotalCount;
                iPosBegin = (iPageCount-1) * iPageSize;
                //iCurrentPage = iPageCount;
                this.bindingNavigatorPositionItem.Text = iPageCount.ToString();
            }
            else iPosEnd = iPosBegin + iPageSize;
            int index = -1;
            dgvAlert.Rows.Clear();
            for (int i = iPosBegin; i < iPosEnd; i++)
            {
                index = dgvAlert.Rows.Add();
                dgvAlert.Rows[index].Cells["Barcode"].Value = dt.Rows[i]["Barcode"];
                dgvAlert.Rows[index].Cells["MerchandiseName"].Value = dt.Rows[i]["MerchandiseName"];
                dgvAlert.Rows[index].Cells["UnitName"].Value = dt.Rows[i]["UnitName"];
                dgvAlert.Rows[index].Cells["TotalStock"].Value = dt.Rows[i]["TotalStock"];
                dgvAlert.Rows[index].Cells["IsAlert"].Value = dt.Rows[i]["IsAlert"];
                dgvAlert.Rows[index].Cells["AlertNum"].Value = dt.Rows[i]["AlertNum"];
            }
        }

        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            this.iCurrentPage = 0;
            BindData();
        }

        private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            if (iCurrentPage >0)
            {
                iCurrentPage -= 1;
                BindData();
            }
        }

        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            if (iCurrentPage < iPageCount-1)
            {
                iCurrentPage += 1;
                BindData();
            }
        }

        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            iCurrentPage = iPageCount - 1;
            BindData();
        }

        private void bindingNavigatorPositionItem_Click(object sender, EventArgs e)
        {
            try
            {
                int i = Convert.ToInt32(bindingNavigatorPositionItem.Text.Trim());
            }
            catch
            {
                bindingNavigatorPositionItem.Text = (iCurrentPage + 1).ToString();
            }
        }

        private void bindingNavigatorPositionItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                int index = Convert.ToInt32(bindingNavigatorPositionItem.Text.Trim());
                if (index < 1 || index > iPageCount)
                {
                    bindingNavigatorPositionItem.Text = (iCurrentPage + 1).ToString();
                }
                else
                {
                    iCurrentPage = (index - 1);
                    BindData();
                }
            }
        }

        private void cbxAll_CheckedChanged(object sender, EventArgs e)
        {
            if (cbxAll.Checked)
            {
                dt = oMerchandiseAlertBAL.MerchandiseStockLowAlert(null, 1);
            }
            else
            {
                dt = oMerchandiseAlertBAL.MerchandiseStockLowAlert(null, 0);
            }
            Init();
        }
        //查询指定Barcode
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSearchText.Text)) return;
            string strBarcode = SystemLibrary.SQLQuoteName(txtSearchText.Text.Trim());
            dt = oMerchandiseAlertBAL.MerchandiseStockLowAlert(strBarcode, 2);
            txtSearchText.SelectAll();
            Init();
        }
        //清除查询内容
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            cbxAll_CheckedChanged(sender, e);
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            string strBarcode = string.Empty;
            bool boolStatus = true;
            try
            {
                int index = dgvAlert.SelectedRows[0].Index;
                strBarcode = dgvAlert.Rows[index].Cells[0].Value.ToString();
                boolStatus = (bool)dgvAlert.Rows[index].Cells["IsAlert"].Value;
            }
            catch { }
            if (string.IsNullOrEmpty(strBarcode))
            {
                e.Cancel = true;
            }
            else {
                if (boolStatus)
                {
                    取消报警ToolStripMenuItem.Visible = true;
                    设置报警ToolStripMenuItem.Visible = false;
                }
                else
                {
                    设置报警ToolStripMenuItem.Visible = true;
                    取消报警ToolStripMenuItem.Visible = false ;
                }
            }
        }

        private void 取消报警ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int index = dgvAlert.SelectedRows[0].Index;
            string strBarcode = dgvAlert.Rows[index].Cells[0].Value.ToString();
            //byte byteIsAlert = (byte)dgvAlert.Rows[index].Cells["IsAlert"].Value;
            int iAlertNum = (int)dgvAlert.Rows[index].Cells["AlertNum"].Value;
            if (MessageBox.Show("您确定要取消" + strBarcode + "的低量报警吗？", "温馨提示",
                MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                if (oMerchandiseAlertBAL.UpdateStockLowAlertStatus(strBarcode, 0, 0))
                {
                    cbxAll_CheckedChanged(sender, e);
                }
            }
        }

        private void 设置报警ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int index = dgvAlert.SelectedRows[0].Index;
            string strBarcode = dgvAlert.Rows[index].Cells[0].Value.ToString();
            //byte byteIsAlert = (byte)dgvAlert.Rows[index].Cells["IsAlert"].Value;
            int iAlertNum = (int)dgvAlert.Rows[index].Cells["AlertNum"].Value;
            if (oMerchandiseAlertBAL.UpdateStockLowAlertStatus(strBarcode, 1, iDefaultAlertNum))
            {
                MessageBox.Show("设置成功", "提示", MessageBoxButtons.OK);
                cbxAll_CheckedChanged(sender, e);
            }
        }

        private void txtSearchText_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                toolStripButton1_Click(sender, new EventArgs());
            }
        }
    }
}
